package com.htg.util;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Build;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.io.File;
import java.security.MessageDigest;

public class UtilCommon {

    private static UtilCommon instance = null;

    public synchronized static UtilCommon getInstance() {
        if (instance == null) {
            instance = new UtilCommon();
        }
        return instance;
    }

    //
    //
    private boolean isClearIng = false;



    //
    public static double[] getLongLatFromBoxString(String boxString) {
        double long1 = 125.06634541;
        double lat1 = 42.81334588;
        double long2 = 125.324429235;
        double lat2 = 43.026597705;

        int left = boxString.indexOf("(");
        int right = boxString.indexOf(")");

        boxString = boxString.substring(left + 1, right);
        boxString = boxString.replace(',', ' ');
        long1 = Double.parseDouble(boxString.split("\\s+")[0]);
        lat1 = Double.parseDouble(boxString.split("\\s+")[1]);
        long2 = Double.parseDouble(boxString.split("\\s+")[2]);
        lat2 = Double.parseDouble(boxString.split("\\s+")[3]);
        return new double[]{long1, lat1, long2, lat2};
    }

    //
    public static double[] getCenterLongLatFromBoxString(String boxString) {
        double[] rect = getLongLatFromBoxString(boxString);
        return new double[]{(rect[0] + rect[2]) / 2, (rect[1] + rect[3]) / 2};
    }

    public static String md5(String str) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(str.getBytes());
            byte[] b = md.digest();
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < b.length; i++) {
                int v = (int) b[i];
                v = v < 0 ? 0x100 + v : v;
                String cc = Integer.toHexString(v);
                if (cc.length() == 1)
                    sb.append('0');
                sb.append(cc);
            }
            return sb.toString();
        } catch (Exception e) {
        }
        return "";
    }


    public static int getViewXOnScreen(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        return location[0];
    }

    public static int getViewYOnScreen(View view) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        return location[1];
    }

    //根据listview内容 更新listview高度
    public static void setListViewHeightBasedOnChildren(ListView listView) {
        // 获取ListView对应的Adapter
        if (listView == null) return;
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = getListViewHeight(listView);
        // listView.getDividerHeight()获取子项间分隔符占用的高度
        // params.height最后得到整个ListView完整显示需要的高度
        listView.setLayoutParams(params);

    }

    public static int getListViewHeight(ListView listView) {
        if (listView == null) {
            return 0;
        }
        ListAdapter adapter = listView.getAdapter();
        if (adapter == null)
            return 0;
        int totalHeight = 0;
        for (int i = 0, len = adapter.getCount(); i < len; i++) { // listAdapter.getCount()返回数据项的数目
            View listItem = adapter.getView(i, null, listView);
            listItem.measure(0, 0); // 计算子项View 的宽高
            totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度
        }

        totalHeight = totalHeight + (listView.getDividerHeight() * (adapter.getCount() - 1));
        return totalHeight;
    }


    //把字符串中的ABCDEF去掉
    public static String removeABCDEF(String temp) {
        if (temp == null) {
            return null;
        }

        temp = temp.replaceAll("[a-zA-Z]", "");
        return temp;
    }

    //截图
    public static Bitmap captureActivity(Activity ctx) {

        View view = ctx.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();

        Bitmap bp = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(),
                view.getMeasuredHeight());

        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();

        return bp;
    }

    //截图
    public static Bitmap captureView(View v) {
        if (null == v) {
            return null;
        }
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache();
        if (Build.VERSION.SDK_INT >= 11) {
            v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(),
                    View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(
                    v.getHeight(), View.MeasureSpec.EXACTLY));
            v.layout((int) v.getX(), (int) v.getY(),
                    (int) v.getX() + v.getMeasuredWidth(),
                    (int) v.getY() + v.getMeasuredHeight());
        } else {
            v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
            v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
        }
        Bitmap b = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());

        v.setDrawingCacheEnabled(false);
        v.destroyDrawingCache();
        return b;
    }

    public static Bitmap loadBitmapFromView(View v) {
        Bitmap b = Bitmap.createBitmap(v.getWidth() , v.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b);
        v.draw(c);
        return b;
    }


    public static boolean isInteger(String str){
        try{
            Integer.parseInt(str);
        }catch (NumberFormatException e){
            return false;
        }catch (NullPointerException e){
            return false;
        }
        return true;
    }



}
